package videoLegend;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import videoLegend.User;
import videoLegend.SQL;


public class Connexion {
	
	private String url;
	static Connection con;

	// new Connexion("mysql", "localhost", 3306, "");
	public Connexion(String db_type, String ip, int port, String db_name){
		this.url = "jdbc:"+db_type+"://"+ip+":"+port+"/"+db_name;
	}
	
	public Connexion(String url){
		this.url = url;
	}
	
	public Connexion() {
		this.url = "jdbc:mysql://localhost:3306/videolegend";
	}
	
	public void openConnexion() throws SQLException, ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection(this.url, "root", "");
	}
	
	public void closeConnexion() throws SQLException{
		con.close();
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public User getUser(String login, String password) throws SQLException{
		PreparedStatement st = con.prepareStatement(SQL.SELECT_USER);
		st.setString(1, login);
		st.setString(2, password);
		ResultSet rs = st.executeQuery();
		if(rs.next()){
			return new User(rs.getInt("id"), rs.getString("login"), rs.getString("password"), rs.getDate("dateInscription"));
		}
		return null;
	}
	
	
	
	public static void main(String args[]){
		/*
		try {
			Connexion connexion = new Connexion("mysql", "localhost", 3306, "sebstore");
			connexion.openConnexion();
			
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery("SELECT * FROM users");
		
		
			while(rs.next()) {
				
				System.out.println(rs.getInt("id"));
				
			}
			
			connexion.closeConnexion();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println(e);
		}

		*/
	}
	
}
